package string;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: asus
 * Date: 2024-12-20
 * Time: 11:00
 */
public class StringDeformation {
    private void reverse(StringBuilder stringBuilder, int begin, int end) {
        while (begin < end) {
            char temp = stringBuilder.charAt(begin);
            stringBuilder.setCharAt(begin, stringBuilder.charAt(end));
            stringBuilder.setCharAt(end, temp);
            begin++;
            end--;
        }
    }
    //h i 4
    public String trans (String s, int n) {
        StringBuilder stringBuilder = new StringBuilder(s);
        for (int i = 0; i < n; i++) {
            char c = stringBuilder.charAt(i);
            if (Character.isUpperCase(c)) {
                stringBuilder.setCharAt(i, Character.toLowerCase(c));
            } else {
                stringBuilder.setCharAt(i, Character.toUpperCase(c));
            }
        }
        reverse(stringBuilder, 0, n-1);
        int fast = 0;
        int low = 0;
        while (fast < n) {
            while (fast < n && stringBuilder.charAt(fast) != ' ') {
                fast++;
            }
            reverse(stringBuilder, low, fast-1);
            while (fast < n && stringBuilder.charAt(fast) == ' ') {
                fast++;
            }
            low = fast;
        }
        return stringBuilder.toString();
    }

    public static void main(String[] args) {
        StringDeformation stringDeformation = new StringDeformation();
        stringDeformation.trans(" this is An aPpLe", 17);
    }
}
